package com.zone.core.ext

import android.content.Context
import android.util.Log
import com.zone.core.base.BaseBindingViewModelActivity
import com.zone.core.base.BaseBindingViewModelFragment
import com.zone.core.base.BaseViewModel
import org.jetbrains.anko.toast

/**
 *Author: last
 *Time: 2022-07-17
 *Description: model 扩展
 */
fun BaseViewModel.bind(context: Context) {
    if (context is BaseBindingViewModelActivity<*, *>) {
        hintText.observe(context) {
            val content = getHintText()
            if (!content.isNullOrBlank()) {
                context.toast(content)
            }
        }
        hintTextRes.observe(
            context
        ) {
            val contentRes = getHintRes()
            if (contentRes > 0) {
                context.toast(contentRes)
            }
        }
        event.observe(context) {
            val eventId = getEventId()
            if (eventId >= 0) {
                context.onEvent(eventId)
            }
        }
    }
}

fun BaseViewModel.bind(fragment: BaseBindingViewModelFragment<*, *>) {
    hintText.observe(fragment.viewLifecycleOwner) {
        val content = getHintText()
        if (!content.isNullOrBlank()) {
            fragment.context?.toast(content)
        }
    }
    hintTextRes.observe(fragment.viewLifecycleOwner) {
        val contentRes = getHintRes()
        if (contentRes > 0) {
            fragment.context?.toast(contentRes)
        }
    }

    event.observe(fragment.viewLifecycleOwner) {
        val eventId = getEventId()
        if (eventId >= 0) {
            fragment.onEvent(eventId)
        }
    }

}